﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.StockLists.SymbolListEnumerators;
using StockFinder.Model;
using StockFinder.Network;
using HtmlAgilityPack;

namespace StockFinder.StockLists.Indexes
{
    public class TelegraphOnlineFtse250StockList : YahooFinanceOnlinePriceLoaderSymbolListEnumerator
    {
        private readonly string[] _Urls = new[]
        {
            "http://shares.telegraph.co.uk/indices/?index=MCX"
        };

        public TelegraphOnlineFtse250StockList()
        {
            Name = "FTSE 250";
        }

        protected override List<Symbol> GetSymbols()
        {
            var stockIndexList = new List<Symbol>();

            foreach (var url in _Urls)
            {
                using (var webClient = new ProxiedWebClient())
                {
                    string pageSource = webClient.DownloadString(url);

                    var doc = new HtmlDocument();
                    doc.LoadHtml(pageSource);

                    var rows = doc.DocumentNode.SelectNodes("//*[@id=\"summary-table\"]/tbody/tr/td[1]/a");

                    foreach (var item in rows)
                    {
                        var ticker = item.InnerText.TrimEnd(new[] { '.' });
                        stockIndexList.Add(new Symbol() { Name = ticker, Exchange = Model.Enumerations.Exchange.LONDON });                        
                    }
                }
            }

            return stockIndexList;
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
